
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.mavenproject1;

/**
 *
 * @author 张飞
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        numberj = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        numberac = new javax.swing.JButton();
        numberjia = new javax.swing.JButton();
        numberjian = new javax.swing.JButton();
        numbercheng = new javax.swing.JButton();
        number1 = new javax.swing.JButton();
        number2 = new javax.swing.JButton();
        number3 = new javax.swing.JButton();
        numberchu = new javax.swing.JButton();
        number4 = new javax.swing.JButton();
        number5 = new javax.swing.JButton();
        number6 = new javax.swing.JButton();
        numberdian = new javax.swing.JButton();
        number7 = new javax.swing.JButton();
        number8 = new javax.swing.JButton();
        number9 = new javax.swing.JButton();
        number0 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("计算器"));

        numberj.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        numberj.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(numberj)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(numberj, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 24, Short.MAX_VALUE))
        );

        jPanel2.setLayout(new java.awt.GridLayout(4, 4));

        numberac.setText("AC");
        numberac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numberacActionPerformed(evt);
            }
        });
        jPanel2.add(numberac);

        numberjia.setText("+");
        numberjia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numberjiaActionPerformed(evt);
            }
        });
        jPanel2.add(numberjia);

        numberjian.setText("-");
        numberjian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numberjianActionPerformed(evt);
            }
        });
        jPanel2.add(numberjian);

        numbercheng.setText("*");
        jPanel2.add(numbercheng);

        number1.setText("1");
        number1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                number1ActionPerformed(evt);
            }
        });
        jPanel2.add(number1);

        number2.setText("2");
        number2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                number2ActionPerformed(evt);
            }
        });
        jPanel2.add(number2);

        number3.setText("3");
        number3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                number3ActionPerformed(evt);
            }
        });
        jPanel2.add(number3);

        numberchu.setText("\\");
            jPanel2.add(numberchu);

            number4.setText("4");
            number4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number4ActionPerformed(evt);
                }
            });
            jPanel2.add(number4);

            number5.setText("5");
            number5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number5ActionPerformed(evt);
                }
            });
            jPanel2.add(number5);

            number6.setText("6");
            number6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number6ActionPerformed(evt);
                }
            });
            jPanel2.add(number6);

            numberdian.setText(".");
            jPanel2.add(numberdian);

            number7.setText("7");
            number7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number7ActionPerformed(evt);
                }
            });
            jPanel2.add(number7);

            number8.setText("8");
            number8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number8ActionPerformed(evt);
                }
            });
            jPanel2.add(number8);

            number9.setText("9");
            number9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number9ActionPerformed(evt);
                }
            });
            jPanel2.add(number9);

            number0.setText("0");
            number0.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    number0ActionPerformed(evt);
                }
            });
            jPanel2.add(number0);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void numberacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numberacActionPerformed
    this.numberj.setText("0");  
    a.ac();
// TODO add your handling code here:
    }//GEN-LAST:event_numberacActionPerformed

    private void numberjiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numberjiaActionPerformed
            // TODO add your handling code here:
            String numberjThing =  numberj.getText();
         try{
            double numberjNumber = Double.parseDouble(numberjThing);
            a.add(numberjNumber);
            numberj.setText(a.getValue()+"");
            newInput = true;
        }catch(Exception e){
            e.printStackTrace();
            //TODO: screenThing represent wrong real number.
        }

    }//GEN-LAST:event_numberjiaActionPerformed

    private void number1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number1ActionPerformed
        // TODO add your handling code here:
         if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("1");
        }else{
            numberj.setText(numberj.getText()+"1");
        }
        newInput = false;

    }//GEN-LAST:event_number1ActionPerformed

    private void number2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number2ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("2");
        }else{
            numberj.setText(numberj.getText()+"2");
        }
        newInput = false;
    }//GEN-LAST:event_number2ActionPerformed

    private void number3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number3ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("3");
        }else{
            numberj.setText(numberj.getText()+"3");
        }
        newInput = false;
    }//GEN-LAST:event_number3ActionPerformed

    private void number4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number4ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("4");
        }else{
            numberj.setText(numberj.getText()+"4");
        }
        newInput = false;
    }//GEN-LAST:event_number4ActionPerformed

    private void number5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number5ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("5");
        }else{
            numberj.setText(numberj.getText()+"5");
        }
        newInput = false;
    }//GEN-LAST:event_number5ActionPerformed

    private void number6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number6ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("6");
        }else{
            numberj.setText(numberj.getText()+"6");
        }
        newInput = false;
    }//GEN-LAST:event_number6ActionPerformed

    private void number7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number7ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("7");
        }else{
            numberj.setText(numberj.getText()+"7");
        }
        newInput = false;
    }//GEN-LAST:event_number7ActionPerformed

    private void number8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number8ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("8");
        }else{
            numberj.setText(numberj.getText()+"8");
        }
        newInput = false;
    }//GEN-LAST:event_number8ActionPerformed

    private void number9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number9ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("9");
        }else{
            numberj.setText(numberj.getText()+"9");
        }
        newInput = false;
    }//GEN-LAST:event_number9ActionPerformed

    private void number0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_number0ActionPerformed
        // TODO add your handling code here:
        if(newInput){
            numberj.setText(0+"");
        }
        
        if(numberj.getText().trim().equals("0")){
            numberj.setText("0");
        }else{
            numberac.setText(numberj.getText()+"0");
        }
        newInput = false;
    }//GEN-LAST:event_number0ActionPerformed

    private void numberjianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numberjianActionPerformed
        // TODO add your handling code here:
         String numberjThing =  numberj.getText();
         try{
            double numberjNumber = Double.parseDouble(numberjThing);
            a.substract(numberjNumber);
            numberj.setText(numberjThing);
            newInput = true;
        }catch(Exception e){
            e.printStackTrace();
    }//GEN-LAST:event_numberjianActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
   private boolean newInput = true;
   NewClass a = new NewClass();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton number0;
    private javax.swing.JButton number1;
    private javax.swing.JButton number2;
    private javax.swing.JButton number3;
    private javax.swing.JButton number4;
    private javax.swing.JButton number5;
    private javax.swing.JButton number6;
    private javax.swing.JButton number7;
    private javax.swing.JButton number8;
    private javax.swing.JButton number9;
    private javax.swing.JButton numberac;
    private javax.swing.JButton numbercheng;
    private javax.swing.JButton numberchu;
    private javax.swing.JButton numberdian;
    private javax.swing.JTextField numberj;
    private javax.swing.JButton numberjia;
    private javax.swing.JButton numberjian;
    // End of variables declaration//GEN-END:variables
}


